#include "stm32f10x.h"                  // Device header
int16_t Encoder_counter;

void Encoder_Init(void)
{
  //开启外设时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启io口时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//开启io口时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//开启AFIO时钟
  //配置IO口
  GPIO_InitTypeDef GPIO_InitStructure;	
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉输入模式
  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;//PB13
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//设置翻转速度
  GPIO_Init(GPIOB,&GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉输入模式
  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;//PB13
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//设置翻转速度
  GPIO_Init(GPIOA,&GPIO_InitStructure);
  //配置中断线
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource7);
  //配置中断
  EXTI_InitTypeDef EXTI_InitStructure;
  EXTI_InitStructure.EXTI_Line=EXTI_Line0|EXTI_Line1|EXTI_Line7;//13号中断线
  EXTI_InitStructure.EXTI_LineCmd=ENABLE;//使能中断线
  EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;//中断模式
  EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;//下降沿
  EXTI_Init(&EXTI_InitStructure);
  //配置中断向量控制器
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断组为2，两位响应优先级，两位抢占优先级
  
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级
  NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//响应优先级
  NVIC_Init(&NVIC_InitStructure);
  
  NVIC_InitStructure.NVIC_IRQChannel=EXTI1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级
  NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;//响应优先级
  NVIC_Init(&NVIC_InitStructure);
  
  NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级
  NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//响应优先级
  NVIC_Init(&NVIC_InitStructure);
}

void EXTI0_IRQHandler(void)
{
  if( EXTI_GetFlagStatus(EXTI_Line0)==SET )
  {
	  if( GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0 )
	  {
	    Encoder_counter++;
	  }
	  EXTI_ClearFlag(EXTI_Line0);
  }
}

void EXTI1_IRQHandler(void)
{
  if( EXTI_GetFlagStatus(EXTI_Line1)==SET )
  {
	  if( GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0 )
	  {
	    Encoder_counter--;
	  }
	  EXTI_ClearFlag(EXTI_Line1);
  }
}

//void EXTI9_5_IRQHandler(void)
//{
//    if( EXTI_GetFlagStatus(EXTI_Line7)==SET )
//  {
//	 if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)==0 )
//	 {
//	   Encoder_counter=0;
//	 }                      
//	 EXTI_ClearFlag(EXTI_Line7);
//  }

//}








